<?php
namespace Admin\Controller;
use Think\Controller;
class GatherController extends CommonController
{	

	// 飞虫表
	public function insect(){
		session('page_size',$_GET['page_size']);

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));
		$shop_id = I('get.shop_id') ? I('get.shop_id') : 1;
		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();
		$timediff = $end_time-$begin_time;
      	$days = intval($timediff/86400);// 相隔多少天用来计算指数

		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;

        $maps['add_time'] = array(array('gt',$begin_time),array('lt',$end_time));
        $maps['shop_id'] = $shop_id;

        $cate_id = C('DEV_LOG_1');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');

		$map['id']=array('in',$animal_id);
		$animal_name=M('animal')->where($map)->field('id,name,ename')->select();

		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->where(array('a.shop_id'=>$shop_id))
			->field('a.id,a.name,a.area')
			// ->field('a.id,a.name,b.number,b.device_id,b.animal_id')
			// ->join('left join zm_feichong_log b on a.id=b.device_id')
			// ->group('id')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) {
			// $data[$k]['feichong_log'] = M('feichong_log')->where('device_id='.$v['id'])->select();

			foreach ($animal_name as $key => $val) {
				$data[$k][$val['ename']] = M('feichong_log')
					->where($maps)
					->where('device_id='.$v['id'].' AND animal_id='.$val['id'])
					->getField('sum(number) as sum_number');

				$data[$k]['sum'] += $data[$k][$val['ename']];
			}
			
		}

		$safe_num_fei = M('configs')->where("ename='safe_num_fei'")->getField('val');
		foreach ($data as $k => $v) {
			$data[$k]['insect'] = sprintf('%.2f',$v['sum']/$days);//飞虫总指数
			$data[$k]['safe'] = (int)$safe_num_fei;
		}
		// dump($data);exit;
		$count =M('device')->where('cate_id='.$cate_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();

        $shop = M('shop')->field('id,shop_name')->select();
        $this->assign("shop", $shop);// 店铺的id

        // get 收到的传回去
        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);
        $this->assign("shop_id", $shop_id);
        $this->assign('type',$_SESSION['type']);
		$this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
		$this ->display();
	}

	// 捕鼠盒
	public function mouseBox(){
		session('page_size',$_GET['page_size']);

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));
		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();

		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;
        $shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
        $cate_id = C('DEV_LOG_2');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');
        //设备查找条件
        $data_where['cate_id'] = $cate_id;
        $data_where['shop_id'] = $shop_id;
        //选取要查找的设备
		$data = M('device a')
			->where($data_where)
			->where(array('a.shop_id'=>$shop_id))
			->field('a.id,a.name,area')
			->page($page_no, $page_size)
			->select();
		$ids = array();


		//循环创造出所有月份的数据
		foreach ($data as $key => $value) {
			$ids[$key] = $value['id'];
	    	for($i=1;$i<=12;$i++){
	    		$data[$key]["yue".$i] = 0;
	    	}
	    }


	    //查找数据
		$where = array();
		//数据的设备id为上面得到的id
		$where['device_id'] = array('in',$ids);
		//查找今年的
		$where['date_y'] =intval(date("Y"));
		$list = M('bait_log a')
	          ->field("a.id,a.device_id,a.number,a.date_m,a.shop_id")
	          ->where($where)
	          ->select();
	    //拼装数据
	   	foreach ($data as $key => $value) {
	   		foreach ($list as $k => $v) {
	   			if($value['id'] == $v['device_id'])
	   				$data[$key]["yue".$v['date_m']] += $v['number'];
	   		}
	   	}
	   	 

		$count =M('device')->where('cate_id='.$cate_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $shop = M('shop')->field('id,shop_name')->select();
        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);
        $this->assign("shop", $shop);// 店铺的id
		$this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign('type',$_SESSION['type']);
        $this->assign("shop_id", $shop_id);
        $this->assign("data", $data);
		$this ->display();
	}

	//粘鼠板
	public function mousePlate(){
		session('page_size',$_GET['page_size']);

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));
		$shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();
		$timediff = $end_time-$begin_time;
      	$days = intval($timediff/86400);// 相隔多少天用来计算指数

		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;

        $maps['add_time'] = array(array('gt',$begin_time),array('lt',$end_time));
        $maps['shop_id'] = $shop_id;

        $cate_id = C('DEV_LOG_3');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');

		$map['id']=array('in',$animal_id);
		$animal_name=M('animal')->where($map)->field('id,name,ename')->select();
		// dump($animal_name);exit();
		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->where(array('a.shop_id'=>$shop_id))
			->field('a.id,a.name,area')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) {
			// $data[$k]['feichong_log'] = M('feichong_log')->where('device_id='.$v['id'])->select();

			foreach ($animal_name as $key => $val) {
				$data[$k][$val['ename']] = M('glue_log')
					->where($maps)
					->where('device_id='.$v['id'].' AND animal_id='.$val['id'])
					->getField('sum(number) as sum_number');

				$data[$k]['sum'] += $data[$k][$val['ename']];
			}
			
		}

		// dump($data);exit();
		$count =M('device')->where('cate_id='.$cate_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $shop = M('shop')->field('id,shop_name')->select();
        $this->assign("shop", $shop);// 店铺的id
        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);
        $this->assign("shop_id", $shop_id);
		$this->assign('page', $show);
		$this->assign('type',$_SESSION['type']);
        $this->assign("count", $count);
        $this->assign("data", $data);
		$this ->display();
	}

	//机械捕鼠器
	public function mouseIron(){
		session('page_size',$_GET['page_size']);

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));

		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();
		$timediff = $end_time-$begin_time;
      	$days = intval($timediff/86400);// 相隔多少天用来计算指数
      	$shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;

        $maps['add_time'] = array(array('gt',$begin_time),array('lt',$end_time));
        $maps['shop_id'] = $shop_id;
        $cate_id = C('DEV_LOG_4');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');

		$map['id']=array('in',$animal_id);
		$animal_name=M('animal')->where($map)->field('id,name,ename')->select();
		// dump($animal_name);exit();
		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->where(array('a.shop_id'=>$shop_id))
			->field('a.id,a.name,area')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) {
			// $data[$k]['feichong_log'] = M('feichong_log')->where('device_id='.$v['id'])->select();

			foreach ($animal_name as $key => $val) {
				$data[$k][$val['ename']] = M('mousetrap_log')
					->where($maps)
					->where('device_id='.$v['id'].' AND animal_id='.$val['id'])
					->getField('sum(number) as sum_number');

				$data[$k]['sum'] += $data[$k][$val['ename']];
			}
			
		}

		// dump($data);exit();
		$count =M('device')->where('cate_id='.$cate_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $shop = M('shop')->field('id,shop_name')->select();
        $this->assign("shop", $shop);// 店铺的id
        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);
        $this->assign("shop_id", $shop_id);
		$this->assign('page', $show);
		$this->assign('type',$_SESSION['type']);
        $this->assign("count", $count);
        $this->assign("data", $data);
		$this ->display();
	}

	//爬虫板
	public function mouseRep(){
		session('page_size',$_GET['page_size']);

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));

		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();
		$timediff = $end_time-$begin_time;
      	$days = intval($timediff/86400);// 相隔多少天用来计算指数
      	$shop_id = $_GET['shop_id'] ? $_GET['shop_id'] : 1;
		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;

        $maps['add_time'] = array(array('gt',$begin_time),array('lt',$end_time));
        $maps['shop_id'] = $shop_id;
        $cate_id = C('DEV_LOG_5');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');

		$map['id']=array('in',$animal_id);
		$animal_name=M('animal')->where($map)->field('id,name,ename')->select();

		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->where(array('a.shop_id'=>$shop_id))
			->field('a.id,a.name,area')
			// ->field('a.id,a.name,b.number,b.device_id,b.animal_id')
			// ->join('left join zm_feichong_log b on a.id=b.device_id')
			// ->group('id')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) {
			// $data[$k]['feichong_log'] = M('feichong_log')->where('device_id='.$v['id'])->select();

			foreach ($animal_name as $key => $val) {
				$data[$k][$val['ename']] = M('reptile_log')
					->where($maps)
					->where('device_id='.$v['id'].' AND animal_id='.$val['id'])
					->getField('sum(number) as sum_number');

				$data[$k]['sum'] += $data[$k][$val['ename']];
			}
			
		}
		// dump($data);exit();
		$safe_num_fei = M('configs')->where("ename='safe_num_pa'")->getField('val');
		foreach ($data as $k => $v) {
			$data[$k]['insect'] = sprintf('%.2f',$v['sum']/$days);//爬虫总指数
			$data[$k]['safe'] = (int)$safe_num_fei;
		}
		// dump($data);exit;
		$count =M('device')->where('cate_id='.$cate_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $shop = M('shop')->field('id,shop_name')->select();
        $this->assign("shop", $shop);// 店铺的id
        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);
        $this->assign("shop_id", $shop_id);
		$this->assign('page', $show);
		$this->assign('type',$_SESSION['type']);
        $this->assign("count", $count);
        $this->assign("data", $data);
		$this ->display();
	}

}